<style>
    .aadl{width: 210px;}
    .aadl dt {width:85px; float: left;}
</style>

<div class="container-fluid ">
    <div class="wrapper">
        <div class="panel" id="nav">
            <div class="panel-heading"><h3 class="panel-title">Shipmentid 不良申请处理</h3></div>
            <div class="panel-body">
                <form action="__URL__/shipmentIdRejectsList" method="get">
                    <div class="container-fluid">
                        <div class="col-sm-12 col-md-3 add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">账号</span>
                                <select type="text" name="account_id" class="form-control chosen-select" tabindex="-1" data-name-group="common">
                                    <option value="">请选择</option>
                                    <volist name="accounts" id="acc">
                                        <option value="{$acc.id}">{$acc.name}</option>
                                    </volist>
                                </select>
                            </div>
                        </div>

                        <div class="col-sm-12 col-md-3 add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">Shipmentid</span>
                                <input class="form-control" type="text" value="" name="shipmentid">
                            </div>
                        </div>

                        <div class="col-sm-12 col-md-2 add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">SKU</span>
                                <input class="form-control" type="text" value="" name="sku">
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-2 add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">销售员</span>
                                <input class="form-control" type="text" value="" name="seller">
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-2 add-space">
                            <div class="input-group input-group-md">
                                <span class="input-group-addon">状态</span>
                                <select type="text" name="status" class="form-control chosen-select" value="">
                                    <option value="">请选择</option>
                                    <volist name="status" id="bs">
                                        <option value="{$Think.lang.$key}">{$bs}</option>
                                    </volist>
                                </select>
                            </div>
                        </div>
                        <div class="col-sm-12 col-md-1 add-space">
                            <button type="submit" class="btn btn-info">查询</button>
                        </div>
                    </div>
                </form>
                <div class="clearfix"></div>
                <hr>
                <table class="table table-striped  table-hover" cellspacing="0" width="auto">
                    <thead>
                    <tr>
                        <th width="20">序号</th>
                        <th>Shipmentid</th>
                        <th>帐号</th>
                        <th>状态</th>
                        <th>SKU</th>
                        <th>SKU中文名</th>
                        <th>总需<br/>求数</th>
                        <th>不良<br/>数量</th>
                        <th>是否<br/>退税</th>
                        <th>主体</th>
                        <th>销售员</th>
                        <th>销售选择</th>
                        <th>日志</th>
                        <th>进良品仓id</th>
                        <th>进不良品仓id</th>
                        <th>备注</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
                    <volist name="data" id="vo" key="num">
                        <tr>
                            <td class="text-center"><input type="hidden" name="num_id" value="{$vo.id}">{$num}</td>
                            <td>{$vo.shipmentid}</td>
                            <td>{$vo.account_name}</td>
                            <td>{$vo.rejects_status_name}</td>
                            <td>{$vo.sku}</td>
                            <td>{$vo.sku_name}</td>
                            <td class="ori_qty">{$vo.ori_qty}</td>
                            <td class="rej_qty">{$vo.rej_qty}</td>
                            <td><if condition="$vo.export_tax_rebate eq 1">是<else />否</if></td>
                            <td>{$vo.enterprise_dominant_name}</td>
                            <td>{$vo.seller_name}</td>
                            <td class="rejects_type">
                                <if condition="$vo.rejects_status eq 10">
                                    <select name="rejects_type" type="text" class="form-control w80">
                                        <option value="">请选择</option>
                                        <volist name="type" id="ty">
                                            <option value="{$Think.lang.$key}">{$ty}</option>
                                        </volist>
                                    </select>
                                <else />
                                    {$vo.rejects_type_name}
                                </if>
                            </td>
                            <td><dl class="aadl">
                                <dt>申请人</dt><dd>{$vo.rejects_apply_user_name}</dd>
                                <dt>申请时间</dt><dd>{$vo.rejects_apply_time}</dd>
                                <dt>处理人</dt><dd>{$vo.decision_user_name}</dd>
                                <dt>处理时间</dt><dd>{$vo.decision_time}</dd>
                                <dt>实物处理人</dt><dd>{$vo.actual_do_user_name}</dd>
                                <dt>实物处理时间</dt><dd>{$vo.actual_do_time}</dd>
                            </dl></td>
                            <td class="warehouse">
                                <if condition="($vo.rejects_status eq 20) and ($vo.rejects_type eq 20) and ($backSaveOk eq 1) and ($vo.warehouseorders_id eq null)">
                                    <a class="addPosition" data-toggle="modal" data-target=".addRelation">分配储位</a>
                                    <input type="hidden" name="warehouse_id" value="">
                                <elseif condition="($vo.rejects_status eq 20) and ($vo.rejects_type eq 10)" />

                                <else />{$vo.warehouseorders_id}
                                </if>
                            </td>
                            <td class="unqualified">
                                <if condition="($vo.rejects_status eq 20) and ($vo.unqualified_deal_invoices_id eq null) and ($backSaveOk eq 1)">
                                    <a class="badDo" data-toggle="modal" data-target="#badDo" onclick="badDo('{$vo.id}')">不良处理</a>
                                    <!--<input type="text" name="unqualified_deal_invoices_id" class="w80 form-control" value="">-->
                                    <else />{$vo.unqualified_deal_invoices_id}
                                </if>
                            </td>
                            <td>{$vo.remark}</td>
                            <td>
                                <input type="hidden" name="site_id" value="{$vo.site_id}">
                                <if condition="($vo.rejects_status eq 10) and ($sellerSaveOk eq 1)">
                                <button type="button" class="btn btn-info btn-sm add-space" onclick="save('{$vo.id}',$(this))">选择确定</button>
                                <elseif condition="($vo.rejects_status eq 20) and ($backSaveOk eq 1) and ($vo.warehouseorders_id eq null) and ($vo.rejects_type eq 20)" />
                                    待分配储位
                                <elseif condition="($vo.rejects_status eq 10)"  />
                                    待销售操作
                                <elseif condition="($vo.rejects_status eq 20) and ($backSaveOk eq 1) and ($vo.unqualified_deal_invoices_id eq null)"  />
                                    待不良处理
                                <else/>
                                    无操作
                                </if>

                            </td>
                        </tr>
                    </volist>

                </table>

                <div class="clearfix clear"></div>
                <div class="col-md-6 pull-left">
                    <label>每页 20 条记录
                        显示 1 到 20 项，共 {$count} 项</label>
                </div>
                <div class="col-md-6">
                    <div class="page pull-right" style="padding: 0;margin: 0">
                        {$page}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<!--良品返仓处理-->
<div class="modal fade addRelation" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">

                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">分配储位</h4>
                </div>
                <div class="modal-body">
                    <table class="table table-striped  table-hover" cellspacing="0" width="100%">
                        <thead>
                        <tr>
                            <th>储位</th>
                            <th>数量</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody id="addHtml">
                        <tr class="">
                            <td><input type="text" name="position[]" class="form-control" /></td>
                            <td><input type="text" name="quantity[]" class="form-control" /></td>
                            <td><a href="javascript:add();">增加</a></td>
                        </tr>
                        </tbody>
                    </table>
                </div>
                <div class="modal-footer">
                    <input type="hidden" value="" name="ori_qty">
                    <input type="hidden" value="" name="rej_qty">
                    <input type="hidden" value="" name="id">
                    <button type="button" class="btn btn-primary save-position">确定</button>
                    <button type="button" class="btn btn-default " data-dismiss="modal" id="mo-close">关闭</button>
                </div>

        </div>
    </div>
</div>
<!--不良返仓处理-->
<div class="modal fade" id="badDo" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2">
    <div class="modal-dialog" role="document">
        <div class="modal-content">

                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" >不良处理</h4>
                </div>
                <div class="modal-body">
                    <div class="form-group form-group-lg">
                        <label class="col-sm-4 control-label text-right">储位</label>
                        <div class="col-sm-8">
                            <input type="text" class="form-control" name="bad_position" placeholder="储位(选填">
                        </div>
                    </div>
                    <!--储位<input type="text" class="form-control" name="position" placeholder="储位">-->
                </div>
                <div class="modal-footer">
                    <input type="hidden" value="" name="rejects_id">
                    <button type="button" class="btn btn-primary bad-position">确定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                </div>

        </div>
    </div>
</div>
<script>
    $(function () {
        $("input[name='shipmentid']").val("<?php  echo $_GET['shipmentid'];?>");
        $("input[name='sku']").val("<?php  echo $_GET['sku'];?>");
        $("select[name='status']").val("<?php echo $_GET['status'];?>");
        $("select[name='account_id']").val("<?php echo $_GET['account_id'];?>");
        $('select.chosen-select').chosen({
            no_results_text: '没有找到',    // 当检索时没有找到匹配项时显示的提示文本
            disable_search_threshold: 2, // 10 个以下的选择项则不显示检索框
            search_contains: true         // 从任意位置开始检索
        });
    });
    //销售选择
    function save(id,isthis) {
        var type = isthis.parent().siblings('.rejects_type').find('select').val();
        var site = isthis.parent().find('input[name="site_id"]').val();
        if(type==''){
            layer.msg('销售选择为空',{icon: 5});
            return ;
        }
        layer.load();
        $.ajax({
            url: "__URL__/rejectsSellerSave",
            type: 'post',
            async: false, //default:
            data: {'id':id,'reject_type':type,'site_id':site},
            dataType: 'json', // default: Intelligent Guess (xml, json, script, or html)
            success: function (data) {
                if ($.trim(data.status) == 200) {
                    layer.msg(data.msg, {icon: 6});
                    parent.location.reload();
                }else{
                    layer.msg(data.msg, {icon: 5});
                }
            }
        });

    }
    //实物返仓
    function back(id,type,isthis) {
        var warehouse_id = '';
        var unqualified_deal_invoices_id = isthis.parent().siblings('.unqualified').find('input').val();
        if(type==20){
            warehouse_id = isthis.parent().siblings('.warehouse').find('input').val();
            var ck = isthis.parent().siblings('.warehouse').children('a').text();
            if($.trim(ck)=='分配储位'){
                layer.msg('请先分配储位',{icon: 5});
                return ;
            }
            if(warehouse_id==''){
                layer.msg('请输入进良品仓id',{icon: 5});
                return ;
            }
        }
        if(unqualified_deal_invoices_id==''){
            layer.msg('请输入进不良品仓id',{icon: 5});
            return ;
        }
        layer.load();
        $.ajax({
            url: "__URL__/rejectsBackSave",
            type: 'post',
            async: false, //default:
            data: {'id':id,'reject_type':type,'warehouseorders_id':warehouse_id,'unqualified_deal_invoices_id':unqualified_deal_invoices_id},
            dataType: 'json', // default: Intelligent Guess (xml, json, script, or html)
            success: function (data) {
                if ($.trim(data.status) == 200) {
                    layer.msg(data.msg, {icon: 6});
                    parent.location.reload();
                }else{
                    layer.msg(data.msg, {icon: 5});
                }
            }
        });
    }
    $('.addPosition').click(function () {
        var ori_qty = parseInt($(this).parent('td').siblings('.ori_qty').text());
        var rej_qty = parseInt($(this).parent('td').siblings('.rej_qty').text());
        var id = parseInt($(this).parent('td').siblings('.text-center').find('input').val());
        $("input[name='ori_qty']").val(ori_qty);
        $("input[name='rej_qty']").val(rej_qty);
        $("input[name='id']").val(id);
    });
    //储位增加
    function add() {
        $("#addHtml").append('<tr><td><input type="text" name="position[]" class="form-control" /></td><td><input type="text" name="quantity[]" class="form-control" /></td><td><a href="javascript:add();">增加</a>  <a class="storageQtyDel" style="cursor: pointer">删除</a></td></tr>');
    }
    //储位删除
    $(document).on("click", ".storageQtyDel", function(){
        $(this).parent().parent().remove();
    });
    //良品处理
    $(document).on("click", ".save-position", function(){
        var msg = '',
            flag = '',
            qty = 0,
            position = [],
            quantity = [],
            ori_qty = $("input[name='ori_qty']").val(),
            rej_qty = $("input[name='rej_qty']").val(),
            id = $("input[name='id']").val(),
            need_qty = parseInt(ori_qty)-parseInt(rej_qty);
        $("input[name='position[]']").each(function () {
            if($(this).val()==''){
                msg +='储位不能为空<br>';
            }else{
                position.push($(this).val());
            }
        });
        $("input[name='quantity[]']").each(function () {
            if($(this).val()==''){
                msg +='数量不能为空<br>';
            }else if(isNaN($(this).val())){
                msg +='数量必须填写数字<br>';
            }
            else{
                qty += parseInt($(this).val());
            }
            quantity.push($(this).val());
        });
        if(msg!=''){
            layer.msg(msg, {icon: 5});
            return false;
        }
        if(qty>need_qty){
            layer.msg('分配数量大于实际数量', {icon: 5});
            return false;
        }
        if(qty<need_qty){
            layer.msg('分配数量小于实际数量', {icon: 5});
            return false;
        }
        $.ajax({
            url: "__URL__/checkPosition",
            type: 'post',
            async: false, //default:
            data: {'position':position},
            dataType: 'json', // default: Intelligent Guess (xml, json, script, or html)
            success: function (data) {
                if ($.trim(data.status) == 200) {
                    flag = 'ok';
                }else{
                    layer.msg(data.msg, {icon: 5});
                    flag = 'no';
                }
            }
        });
        if(flag == 'no'){
            return false;
        }
        if(msg==''){
            $.ajax({
                url: "__URL__/rejectsBackSave",
                type: 'post',
                async: false, //default:
                data: {'id':id,'position':position,'quantity':quantity},
                dataType: 'json', // default: Intelligent Guess (xml, json, script, or html)
                success: function (data) {
                    if ($.trim(data.status) == 200) {
                        layer.msg(data.msg, {icon: 6});
                        parent.location.reload();
                    }else{
                        layer.msg(data.msg, {icon: 5});
                    }
                }
            });
        }else{
            layer.msg(msg,{icon:5});
        }
    });
    //不良赋值
    function badDo(id) {
        $("input[name='rejects_id']").val(id);
    }
    //不良处理
    $('.bad-position').click(function () {
        var id = $("input[name='rejects_id']").val();
        var position = $("input[name='bad_position']").val();
        if($.trim(id)!=''){
            $.ajax({
                url: "__URL__/badProductsSave",
                type: 'post',
                async: false, //default:
                data: {'id':id,'position':position},
                dataType: 'json', // default: Intelligent Guess (xml, json, script, or html)
                success: function (data) {
                    if ($.trim(data.status) == 200) {
                        layer.msg(data.msg, {icon: 6});
                        parent.location.reload();
                    }else{
                        layer.msg(data.msg, {icon: 5});
                    }
                }
            });
        }else{
            layer.msg('参数错误', {icon: 5});
        }
    });
</script>